<?php

class calendar Extends MY_Controller{
	
	public function __construct()
	{
		parent::__construct();
		
		$this->page_auth->secure_output_page();
		$this->view_data['system_message'] = $this->_msg();
		$prefs['template'] = '

		   {table_open}<table class="calendar" border="1" cellpadding="10" cellspacing="0">{/table_open}

		   {heading_row_start}<tr>{/heading_row_start}
		   {heading_previous_cell}<th><a href="{previous_url}" class="btn"><i class="icon-step-backward"></i></a></th>{/heading_previous_cell}
		   {heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
		   {heading_next_cell}<th><a href="{next_url}" class="btn"><i class="icon-step-forward"></i></a></th>{/heading_next_cell}
		   {heading_row_end}</tr>{/heading_row_end}
		   
		   
		   {week_row_start}<tr>{/week_row_start}
		   {week_day_cell}<td class="center"><span class="label">{week_day}</span></td>{/week_day_cell}
		   {week_row_end}</tr>{/week_row_end}

		   {cal_row_start}<tr>{/cal_row_start}
		   {cal_cell_start}<td class="day-format">{/cal_cell_start}

		   {cal_cell_content}<span class="badge badge-info">{day}</span>{content}{/cal_cell_content}
		   {cal_cell_content_today}<span class="badge badge-inverse">{day}</span>{content}{/cal_cell_content_today}

		   {cal_cell_no_content}<span class="badge badge-info">{day}</span>{/cal_cell_no_content}
		   {cal_cell_no_content_today}<span class="badge badge-inverse">{day}</span>{/cal_cell_no_content_today}

		   {cal_cell_blank}&nbsp;{/cal_cell_blank}

		   {cal_cell_end}</td>{/cal_cell_end}
		   {cal_row_end}</tr>{/cal_row_end}

		   {table_close}</table>{/table_close}';
		
		$prefs['start_day']    = 'sunday';
		$prefs['month_type']   ='long';
		$prefs['day_type']     ='long';
		$prefs['show_next_prev']  =TRUE;
		$prefs['next_prev_url'] = site_url().$this->router->class.'/'.$this->router->method;
		$this->calendar_config = $prefs;

		//create month for dropdown
		$_months = ['january','february','march','april','may','june','july','august','september','october','november','december'];
		foreach($_months as $k => $v)
		{
			$months[$k+1] = ucwords($v);
		}
		$this->view_data['months'] = $months;

		//create year for dropdown
		foreach(range(2009,date('Y')+3) as $v)
		{
			$years[$v] = $v;
		}
		$this->view_data['years'] = $years;



	}

	public function index()
	{

	}

	public function academic($year ='',$month ='')
	{
		$this->layout_view = 'application_less_sidebar';
		//layout of page
		$this->layout_view = 'application_less_sidebar';
		//if year is empty current year
		$year = $year == '' ? date('Y',time()) : $year;
		//if month is empty current month
		$month = $month == '' ? date('m',time()) : $month;

		$this->load->model('M_Calendar','mcal');
		$this->load->library('calendar',$this->calendar_config);

		$event = $this->mcal->get_all_events($year,$month,'for_academic');

		$current_url = 'calendar-academic-'.$year.'-'.$month;
		$this->view_data['calendar'] = $this->calendar->generate_event_calendar($year,$month,$event,$current_url);
		$this->view_data['current_month'] = $month;
		$this->view_data['current_year'] = $year;
		

		$this->view_data['plink'] = site_url('calendar/academic');

		if($this->input->post('add_new_evt_for_acad_cal'))
		{
			$post = $this->input->post();
			$stat = $this->mcal->new_event($post,'academic');

			if($stat->stat == TRUE)
			{
				$this->_msg('s',$stat->msg,'calendar/academic/'.$year.'/'.$month);
			}else{
				$this->_msg('e',$stat->msg,'calendar/academic/'.$year.'/'.$month);
			}
		}
	}

	public function school($year ='',$month ='')
	{
		//layout of page
		$this->layout_view = 'application_less_sidebar';
		//if year is empty current year
		$year = $year == '' ? date('Y',time()) : $year;
		//if month is empty current month
		$month = $month == '' ? date('m',time()) : $month;

		$this->load->model('M_Calendar','mcal');
		$this->load->library('calendar',$this->calendar_config);

		$event = $this->mcal->get_all_events($year,$month);
		$current_url = 'calendar-school-'.$year.'-'.$month;
		$this->view_data['calendar'] = $this->calendar->generate_event_calendar($year,$month,$event,$current_url);
		$this->view_data['current_month'] = $month;
		$this->view_data['current_year'] = $year;

		$this->view_data['plink'] = site_url('calendar/school');
		

		if($this->input->post('add_new_evt_for_school_cal'))
		{
			$post = $this->input->post();
			$stat = $this->mcal->new_event($post);

			if($stat->stat == TRUE)
			{
				$this->_msg('s',$stat->msg,'calendar/school/'.$year.'/'.$month);
			}else{
				$this->_msg('e',$stat->msg,'calendar/school/'.$year.'/'.$month);
			}
		}
	}


	public function section($year ='',$month ='',$section_id = FALSE)
	{
		$this->layout_view = 'application_less_sidebar';

		//layout of page
		$this->layout_view = 'application_less_sidebar';
		//if year is empty current year
		$year = $year == '' ? date('Y',time()) : $year;
		//if month is empty current month
		$month = $month == '' ? date('m',time()) : $month;
		
		$this->view_data['current_month'] = $month;
		$this->view_data['current_year'] = $year;
		$this->view_data['plink'] = site_url('calendar/section');
		$this->view_data['clink'] = site_url('calendar/section/'.$year.'/'.$month);

		$this->load->model('M_Calendar','mcal');
		$this->load->model('M_sections','msec');
		//get sections for dropdown
		$this->view_data['sections'] = $this->msec->get_section_for_dd();

		//if section id exist and section id is official or verified
		if($section_id && $this->msec->verify_section($section_id))
		{
			$this->calendar_config['template'] = '

			   {table_open}<table class="calendar" border="1" cellpadding="10" cellspacing="0">{/table_open}

			   {heading_row_start}<tr>{/heading_row_start}
			   {heading_previous_cell}<th><a href="{previous_url}/'.$section_id.'" class="btn"><i class="icon-step-backward"></i></a></th>{/heading_previous_cell}
			   {heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
			   {heading_next_cell}<th><a href="{next_url}/'.$section_id.'" class="btn"><i class="icon-step-forward"></i></a></th>{/heading_next_cell}
			   {heading_row_end}</tr>{/heading_row_end}
			   
			   
			   {week_row_start}<tr>{/week_row_start}
			   {week_day_cell}<td class="center"><span class="label">{week_day}</span></td>{/week_day_cell}
			   {week_row_end}</tr>{/week_row_end}

			   {cal_row_start}<tr>{/cal_row_start}
			   {cal_cell_start}<td class="day-format">{/cal_cell_start}

			   {cal_cell_content}<span class="badge badge-info">{day}</span>{content}{/cal_cell_content}
			   {cal_cell_content_today}<span class="badge badge-inverse">{day}</span>{content}{/cal_cell_content_today}

			   {cal_cell_no_content}<span class="badge badge-info">{day}</span>{/cal_cell_no_content}
			   {cal_cell_no_content_today}<span class="badge badge-inverse">{day}</span>{/cal_cell_no_content_today}

			   {cal_cell_blank}&nbsp;{/cal_cell_blank}

			   {cal_cell_end}</td>{/cal_cell_end}
			   {cal_row_end}</tr>{/cal_row_end}

			   {table_close}</table>{/table_close}';


			$this->view_data['modal'] = TRUE;
			$this->view_data['current_section'] = $section_id;
			// $this->calendar_config['show_next_prev'] = FALSE;
			$this->load->library('calendar',$this->calendar_config);


			$event = $this->mcal->get_all_events($year,$month,'for_section',$section_id);

			$current_url = 'calendar-section-'.$year.'-'.$month.'-'.$section_id;
			$this->view_data['calendar'] = $this->calendar->generate_event_calendar($year,$month,$event,$current_url);

			

			if($this->input->post('add_new_evt_for_section_cal'))
			{
				$post = $this->input->post();
				$stat = $this->mcal->new_event($post,'section');

				if($stat->stat == TRUE)
				{
					$this->_msg('s',$stat->msg,'calendar/section/'.$year.'/'.$month.'/'.$section_id);
				}else{
					$this->_msg('e',$stat->msg,'calendar/section'.$year.'/'.$month.'/'.$section_id);
				}
			}

		}else{
			$this->view_data['calendar'] = $this->_msg('n','Please Select a Section');
			$this->view_data['modal'] = FALSE;
			$this->view_data['current_section'] = 0;
		}
	}

	public function editevent($event_batch_id = '')
	{

		$this->layout_view = 'application_less_sidebar';
		$this->load->model('M_calendar','mcal');
		$this->view_data['event'] = $event = $this->mcal->get_event($event_batch_id);
		$this->view_data['batch_id'] = $event_batch_id;


		if($this->input->post('edit_this_event_right_now'))
		{
			if($this->mcal->edit_event($event,$this->input->post()))
			{
				$this->_msg('s','Your changes were saved.','calendar/editevent/'.$event_batch_id);
			}else{
				$this->_msg('e','Unable to save your changes, please try again later.','calendar/editevent/'.$event_batch_id);
			}
		}
	}
}

/*<div style="position: absolute; z-index: 999; width: 500px;">asdf</div>*/